package com.kzd.btgame.main.home

import androidx.core.widget.addTextChangedListener
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityHomeCouponBinding
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.home.adapter.CouponAreaAdapter
import com.kzd.btgame.main.home.adapter.CouponEnjoyAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class HomeCouponActivity : OwnerViewBindingActivity<ActivityHomeCouponBinding>() {

    private val mCouponEnjoyAdapter = CouponEnjoyAdapter()

    private val mCouponAreaAdapter = CouponAreaAdapter()

    private var mAppSearchContent: String? = null

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityHomeCouponBinding
        get() = ActivityHomeCouponBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flHomeCouponBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.rvHomeCouponEnjoyBody.apply {
            val layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            this.layoutManager = layoutManager

            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State,
                ) {
                    val position: Int = parent.getChildAdapterPosition(view)
                    if (position == 0) {
                        outRect.left = ConvertUtils.dp2px(15f)
                    }
                    if (position == state.itemCount - 1) {
                        outRect.right = ConvertUtils.dp2px(15f)
                    } else {
                        outRect.right = ConvertUtils.dp2px(15f)
                    }
                }
            })
        }.adapter = mCouponEnjoyAdapter

        mViewBinding.rvHomeCouponAreaBody.adapter = mCouponAreaAdapter


        mViewBinding.includeAppSearch.etAppSearchContent.addTextChangedListener {
            mAppSearchContent = it?.toString()
        }

        mViewBinding.includeAppSearch.tvAppSearchSubmit.setOnClickAntiShakeListener {
            mViewModel.homeCouponArea(mAppSearchContent)
        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.homeCouponArea(mAppSearchContent)
        }
        mViewBinding.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.homeCouponArea(mAppSearchContent, isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.homeCouponEnjoy()
        mViewModel.homeCouponArea()
    }

    override fun initViewMode() {

        mViewModel.homeCouponEnjoyLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                mViewBinding.llHomeCouponEnjoy.visibility = View.GONE
            } else if (response.status == Status.SUCCESS) {
                mViewBinding.llHomeCouponEnjoy.visibility = View.VISIBLE
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.size == 0) {
                mViewBinding.llHomeCouponEnjoy.visibility = View.GONE
            }
            mCouponEnjoyAdapter.setNewInstance(data)
        })

        mViewModel.homeCouponAreaLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
                mViewBinding.llHomeCouponArea.visibility = View.GONE
                mViewBinding.mSmartRefreshLayout.finishRefresh(false)
                mViewBinding.mSmartRefreshLayout.finishLoadMore(false)
            } else if (response.status == Status.SUCCESS) {
                mViewBinding.llHomeCouponArea.visibility = View.VISIBLE
                mViewBinding.mSmartRefreshLayout.finishRefresh(true)
                mViewBinding.mSmartRefreshLayout.finishLoadMore(true)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data
                ?: return@observe
            if (data.isLoadMore) {
                mCouponAreaAdapter.addData(data.data)
            } else {
                if (data.data.size <= 0) {
                    mViewBinding.llHomeCouponArea.visibility = View.GONE
                }
                mCouponAreaAdapter.setNewInstance(data.data)
            }
        })
    }

}